
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>使用 Git 和 GitHub 工作 &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" />
    <link rel="next" title="JavaScript" href="javascript.html" />
    <link rel="prev" title="提交补丁" href="submitting-patches.html" />



 
<script src="../../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="submitting-patches.html" title="提交补丁">previous</a>
     |
    <a href="../../index.html" title="Django internals" accesskey="U">up</a>
   |
    <a href="javascript.html" title="JavaScript">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="internals-contributing-writing-code-working-with-git">
            
  <div class="section" id="s-working-with-git-and-github">
<span id="working-with-git-and-github"></span><h1>使用 Git 和 GitHub 工作<a class="headerlink" href="#working-with-git-and-github" title="永久链接至标题">¶</a></h1>
<p>This section explains how the community can contribute code to Django via pull
requests. If you're interested in how committers handle them, see
<a class="reference internal" href="../committing-code.html"><span class="doc">提交代码</span></a>.</p>
<p>Below, we are going to show how to create a GitHub pull request containing the
changes for Trac ticket #xxxxx. By creating a fully-ready pull request, you
will make the reviewer's job easier, meaning that your work is more likely to
be merged into Django.</p>
<p>您也可以将传统的补丁程序上传到 Trac ，但对于评论来说不太实用。</p>
<div class="section" id="s-installing-git">
<span id="installing-git"></span><h2>安装 Git<a class="headerlink" href="#installing-git" title="永久链接至标题">¶</a></h2>
<p>Django uses <a class="reference external" href="https://git-scm.com/">Git</a> for its source control. You can <a class="reference external" href="https://git-scm.com/download">download</a> Git, but it's often easier to install with
your operating system's package manager.</p>
<p>Django的 <a class="reference external" href="https://github.com/django/django/">Git 仓库</a>  托管在  <a class="reference external" href="https://github.com/">GitHub</a> 上，建议您也使用GitHub。</p>
<p>After installing Git, the first thing you should do is setup your name and
email:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ git config --global user.name &quot;Your Real Name&quot;
$ git config --global user.email &quot;you@email.com&quot;
</pre></div>
</div>
<p>请注意， <code class="docutils literal notranslate"><span class="pre">user.name</span></code> 应该是您的真实姓名，而不是您的GitHub昵称。 GitHub应该知道您在 <code class="docutils literal notranslate"><span class="pre">user.email</span></code> 字段中使用的电子邮件，因为这将用于将提交与GitHub账户相关联。</p>
</div>
<div class="section" id="s-setting-up-local-repository">
<span id="setting-up-local-repository"></span><h2>设置本地仓库<a class="headerlink" href="#setting-up-local-repository" title="永久链接至标题">¶</a></h2>
<p>When you have created your GitHub account, with the nick &quot;GitHub_nick&quot;, and
<a class="reference external" href="https://github.com/django/django/fork">forked Django's repository</a>,
create a local copy of your fork:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">clone</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">github</span><span class="o">.</span><span class="n">com</span><span class="o">/</span><span class="n">GitHub_nick</span><span class="o">/</span><span class="n">django</span><span class="o">.</span><span class="n">git</span>
</pre></div>
</div>
<p>This will create a new directory &quot;django&quot;, containing a clone of your GitHub
repository. The rest of the git commands on this page need to be run within the
cloned directory, so switch to it now:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">cd</span> <span class="n">django</span>
</pre></div>
</div>
<p>在 Git 中，你的 Github 仓库会被称做“origin”。</p>
<p>You should also setup <code class="docutils literal notranslate"><span class="pre">django/django</span></code> as an &quot;upstream&quot; remote (that is, tell
git that the reference Django repository was the source of your fork of it):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">remote</span> <span class="n">add</span> <span class="n">upstream</span> <span class="n">git</span><span class="nd">@github</span><span class="o">.</span><span class="n">com</span><span class="p">:</span><span class="n">django</span><span class="o">/</span><span class="n">django</span><span class="o">.</span><span class="n">git</span>
<span class="n">git</span> <span class="n">fetch</span> <span class="n">upstream</span>
</pre></div>
</div>
<p>您可以类似地添加其他远程仓库，例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">remote</span> <span class="n">add</span> <span class="n">akaariai</span> <span class="n">git</span><span class="nd">@github</span><span class="o">.</span><span class="n">com</span><span class="p">:</span><span class="n">akaariai</span><span class="o">/</span><span class="n">django</span><span class="o">.</span><span class="n">git</span>
</pre></div>
</div>
</div>
<div class="section" id="s-working-on-a-ticket">
<span id="working-on-a-ticket"></span><h2>在工单上工作<a class="headerlink" href="#working-on-a-ticket" title="永久链接至标题">¶</a></h2>
<p>When working on a ticket, create a new branch for the work, and base that work
on <code class="docutils literal notranslate"><span class="pre">upstream/main</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">checkout</span> <span class="o">-</span><span class="n">b</span> <span class="n">ticket_xxxxx</span> <span class="n">upstream</span><span class="o">/</span><span class="n">main</span>
</pre></div>
</div>
<p>-b 参数在本地为您创建一个新分支。 即使是最小的事情，也不要犹豫创建新分支——这就是它们的用途。</p>
<p>相反，如果您正在1.4分支上进行修复，则可以执行以下操作：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">checkout</span> <span class="o">-</span><span class="n">b</span> <span class="n">ticket_xxxxx_1_4</span> <span class="n">upstream</span><span class="o">/</span><span class="n">stable</span><span class="o">/</span><span class="mf">1.4</span><span class="o">.</span><span class="n">x</span>
</pre></div>
</div>
<p>Assume the work is carried on the ticket_xxxxx branch. Make some changes and
commit them:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">commit</span>
</pre></div>
</div>
<p>编写提交消息时，请遵循提交 <a class="reference internal" href="../committing-code.html#committing-guidelines"><span class="std std-ref">commit message guidelines</span></a> 以简化提交者的工作。 如果您不习惯使用英语，请尝试至少准确描述提交的功能。</p>
<p>如果您需要在分支上执行其他工作，请根据需要多次提交：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">commit</span> <span class="o">-</span><span class="n">m</span> <span class="s1">&#39;Added two more tests for edge cases&#39;</span>
</pre></div>
</div>
<div class="section" id="s-publishing-work">
<span id="publishing-work"></span><h3>发布工作成果<a class="headerlink" href="#publishing-work" title="永久链接至标题">¶</a></h3>
<p>You can publish your work on GitHub by running:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">push</span> <span class="n">origin</span> <span class="n">ticket_xxxxx</span>
</pre></div>
</div>
<p>When you go to your GitHub page, you will notice a new branch has been created.</p>
<p>If you are working on a Trac ticket, you should mention in the ticket that
your work is available from branch ticket_xxxxx of your GitHub repo. Include a
link to your branch.</p>
<p>请注意，在Git中，上述分支称为“主题分支”。 您可以自由地重写该分支的历史记录，例如，使用 <code class="docutils literal notranslate"><span class="pre">git</span> <span class="pre">rebase</span></code> 变更基本。 其他人不应在这样的分支上进行工作，因为当您编辑提交时，他们的副本会损坏。</p>
<p>There are also &quot;public branches&quot;. These are branches other people are supposed
to fork, so the history of these branches should never change. Good examples
of public branches are the <code class="docutils literal notranslate"><span class="pre">main</span></code> and <code class="docutils literal notranslate"><span class="pre">stable/A.B.x</span></code> branches in the
<code class="docutils literal notranslate"><span class="pre">django/django</span></code> repository.</p>
<p>当您准备将工作成果并入Django时，应在GitHub上创建一个pull请求。好的拉取请求要求：</p>
<ul class="simple">
<li>按照:doc:<cite>coding style &lt;coding-style&gt;</cite> ，每个提交都进行一次逻辑更改，</li>
<li>每次提交的格式正确的消息：摘要行，其后以72个字符包装的段落——有关详细信息，请参见 <a class="reference internal" href="../committing-code.html#committing-guidelines"><span class="std std-ref">committing guidelines</span></a> 。</li>
<li>如果需要，带有文档和测试——实际上一直需要测试，除了文档更改外。</li>
</ul>
<p>测试套件必须通过，并且文档的构建必须没有警告。</p>
<p>Once you have created your pull request, you should add a comment in the
related Trac ticket explaining what you've done. In particular, you should note
the environment in which you ran the tests, for instance: &quot;all tests pass
under SQLite and MySQL&quot;.</p>
<p>Pull requests at GitHub have only two states: open and closed. The committer
who will deal with your pull request has only two options: merge it or close
it. For this reason, it isn't useful to make a pull request until the code is
ready for merging -- or sufficiently close that a committer will finish it
themselves.</p>
</div>
<div class="section" id="s-rebasing-branches">
<span id="rebasing-branches"></span><h3>分支变基<a class="headerlink" href="#rebasing-branches" title="永久链接至标题">¶</a></h3>
<p>In the example above, you created two commits, the &quot;Fixed ticket_xxxxx&quot; commit
and &quot;Added two more tests&quot; commit.</p>
<p>我们不想在您的代码库中拥有您工作过程的全部历史记录。您的提交“添加了两个以上的测试”将毫无助益。相反，我们只希望一次提交包含您的所有工作。</p>
<p>要重做分支的历史记录，可以使用交互式rebase将提交压缩为一个：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">rebase</span> <span class="o">-</span><span class="n">i</span> <span class="n">HEAD</span><span class="o">~</span><span class="mi">2</span>
</pre></div>
</div>
<p>上面的 HEAD~2 是最近两次提交的简写。 上面的命令将打开一个编辑器，显示两个提交，前缀为 &quot;pick&quot; 。</p>
<p>Change &quot;pick&quot; on the second line to &quot;squash&quot; instead. This will keep the
first commit, and squash the second commit into the first one. Save and quit
the editor. A second editor window should open, so you can reword the
commit message for the commit now that it includes both your steps.</p>
<p>您还可以在rebase中使用“编辑”选项。这样，您可以更改单个提交，例如修复文档字符串中的错字：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">rebase</span> <span class="o">-</span><span class="n">i</span> <span class="n">HEAD</span><span class="o">~</span><span class="mi">3</span>
<span class="c1"># Choose edit, pick, pick for the commits</span>
<span class="c1"># Now you are able to rework the commit (use git add normally to add changes)</span>
<span class="c1"># When finished, commit work with &quot;--amend&quot; and continue</span>
<span class="n">git</span> <span class="n">commit</span> <span class="o">--</span><span class="n">amend</span>
<span class="c1"># Reword the commit message if needed</span>
<span class="n">git</span> <span class="n">rebase</span> <span class="o">--</span><span class="k">continue</span>
<span class="c1"># The second and third commits should be applied.</span>
</pre></div>
</div>
<p>If your topic branch is already published at GitHub, for example if you're
making minor changes to take into account a review, you will need to force-push
the changes:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">push</span> <span class="o">-</span><span class="n">f</span> <span class="n">origin</span> <span class="n">ticket_xxxxx</span>
</pre></div>
</div>
<p>Note that this will rewrite history of ticket_xxxxx - if you check the commit
hashes before and after the operation at GitHub you will notice that the commit
hashes do not match anymore. This is acceptable, as the branch is a topic
branch, and nobody should be basing their work on it.</p>
</div>
<div class="section" id="s-after-upstream-has-changed">
<span id="after-upstream-has-changed"></span><h3>在上游发生变化之后<a class="headerlink" href="#after-upstream-has-changed" title="永久链接至标题">¶</a></h3>
<p>When upstream (<code class="docutils literal notranslate"><span class="pre">django/django</span></code>) has changed, you should rebase your work. To
do this, use:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">fetch</span> <span class="n">upstream</span>
<span class="n">git</span> <span class="n">rebase</span>
</pre></div>
</div>
<p>The work is automatically rebased using the branch you forked on, in the
example case using <code class="docutils literal notranslate"><span class="pre">upstream/main</span></code>.</p>
<p>rebase命令暂时删除所有本地提交，应用上游提交，然后在工作上再次应用本地提交。</p>
<p>If there are merge conflicts, you will need to resolve them and then use <code class="docutils literal notranslate"><span class="pre">git</span>
<span class="pre">rebase</span> <span class="pre">--continue</span></code>. At any point you can use <code class="docutils literal notranslate"><span class="pre">git</span> <span class="pre">rebase</span> <span class="pre">--abort</span></code> to return
to the original state.</p>
<p>请注意，您想在上游 <em>变基</em> ，而不是 <em>合并</em> 上游。</p>
<p>这样做的原因是，通过重新定基，您的提交将始终在上游工作的*基础*上，而不是与上游所做的更改*混合在一起*。这样，您的分支将只包含与其主题相关的提交，这使得压缩更加容易。</p>
</div>
<div class="section" id="s-after-review">
<span id="after-review"></span><h3>审阅之后<a class="headerlink" href="#after-review" title="永久链接至标题">¶</a></h3>
<p>在审核者未要求更改的情况下，将任何不重要的代码放入内核是很不正常的。 在这种情况下，通常最好将更改添加为对工作的一次增量提交。 这使审阅者可以轻松地检查您所做的更改。</p>
<p>在这种情况下，请执行审阅者要求的更改。 尽可能频繁地提交。 在发布更改之前，请重新设定工作基础。 如果添加了两个提交，则将运行：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">rebase</span> <span class="o">-</span><span class="n">i</span> <span class="n">HEAD</span><span class="o">~</span><span class="mi">2</span>
</pre></div>
</div>
<p>将第二个提交压缩为第一个。 按照以下内容编写提交消息：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Made</span> <span class="n">changes</span> <span class="n">asked</span> <span class="ow">in</span> <span class="n">review</span> <span class="n">by</span> <span class="o">&lt;</span><span class="n">reviewer</span><span class="o">&gt;</span>

<span class="o">-</span> <span class="n">Fixed</span> <span class="n">whitespace</span> <span class="n">errors</span> <span class="ow">in</span> <span class="n">foobar</span>
<span class="o">-</span> <span class="n">Reworded</span> <span class="n">the</span> <span class="n">docstring</span> <span class="n">of</span> <span class="n">bar</span><span class="p">()</span>
</pre></div>
</div>
<p>Finally, push your work back to your GitHub repository. Since you didn't touch
the public commits during the rebase, you should not need to force-push:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">push</span> <span class="n">origin</span> <span class="n">ticket_xxxxx</span>
</pre></div>
</div>
<p>您的拉取请求现在也应该包含新的提交。</p>
<p>请注意，提交者在提交代码时可能会将审阅提交压缩为先前的提交。</p>
</div>
</div>
<div class="section" id="s-working-on-a-patch">
<span id="working-on-a-patch"></span><h2>Working on a patch<a class="headerlink" href="#working-on-a-patch" title="永久链接至标题">¶</a></h2>
<p>One of the ways that developers can contribute to Django is by reviewing
patches. Those patches will typically exist as pull requests on GitHub and
can be easily integrated into your local repository:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">git</span> <span class="n">checkout</span> <span class="o">-</span><span class="n">b</span> <span class="n">pull_xxxxx</span> <span class="n">upstream</span><span class="o">/</span><span class="n">main</span>
<span class="n">curl</span> <span class="n">https</span><span class="p">:</span><span class="o">//</span><span class="n">github</span><span class="o">.</span><span class="n">com</span><span class="o">/</span><span class="n">django</span><span class="o">/</span><span class="n">django</span><span class="o">/</span><span class="n">pull</span><span class="o">/</span><span class="n">xxxxx</span><span class="o">.</span><span class="n">patch</span> <span class="o">|</span> <span class="n">git</span> <span class="n">am</span>
</pre></div>
</div>
<p>This will create a new branch and then apply the changes from the pull request
to it. At this point you can run the tests or do anything else you need to
do to investigate the quality of the patch.</p>
<p>For more detail on working with pull requests see the
<a class="reference internal" href="../committing-code.html#handling-pull-requests"><span class="std std-ref">guidelines for committers</span></a>.</p>
</div>
<div class="section" id="s-summary">
<span id="summary"></span><h2>概览<a class="headerlink" href="#summary" title="永久链接至标题">¶</a></h2>
<ul class="simple">
<li>如果可以，请在GitHub上工作。</li>
<li>通过链接到您的GitHub分支来宣布您在Trac工单上的工作。</li>
<li>准备就绪后，请提出拉取请求。</li>
<li>使您的拉取请求尽可能地好。</li>
<li>在对您的工作进行修复时，请使用 <code class="docutils literal notranslate"><span class="pre">git</span> <span class="pre">rebase</span> <span class="pre">-i</span></code> 压缩提交。</li>
<li>当上游发生变化时，请执行 <code class="docutils literal notranslate"><span class="pre">git</span> <span class="pre">fetch</span> <span class="pre">upstream;</span> <span class="pre">git</span> <span class="pre">rebase</span></code>。</li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">使用 Git 和 GitHub 工作</a><ul>
<li><a class="reference internal" href="#installing-git">安装 Git</a></li>
<li><a class="reference internal" href="#setting-up-local-repository">设置本地仓库</a></li>
<li><a class="reference internal" href="#working-on-a-ticket">在工单上工作</a><ul>
<li><a class="reference internal" href="#publishing-work">发布工作成果</a></li>
<li><a class="reference internal" href="#rebasing-branches">分支变基</a></li>
<li><a class="reference internal" href="#after-upstream-has-changed">在上游发生变化之后</a></li>
<li><a class="reference internal" href="#after-review">审阅之后</a></li>
</ul>
</li>
<li><a class="reference internal" href="#working-on-a-patch">Working on a patch</a></li>
<li><a class="reference internal" href="#summary">概览</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="submitting-patches.html"
                        title="上一章">提交补丁</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="javascript.html"
                        title="下一章">JavaScript</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/internals/contributing/writing-code/working-with-git.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="submitting-patches.html" title="提交补丁">previous</a>
     |
    <a href="../../index.html" title="Django internals" accesskey="U">up</a>
   |
    <a href="javascript.html" title="JavaScript">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>